//
//  UILabel+Conveniently.m
//  app
//
//  Created by 刘华舟 on 15/6/19.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "UILabel+Conveniently.h"

@implementation UILabel (Conveniently)

//文字居上显示
- (void)labelTextAlignTop{
    CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++){
        self.text = [self.text stringByAppendingString:@"\n "];
    }
    
}

//文字居下显示
- (void)labelTextAlignBottom{
    CGSize fontSize = [self.text sizeWithAttributes:@{NSFontAttributeName:self.font}];
    double finalHeight = fontSize.height * self.numberOfLines;
    double finalWidth = self.frame.size.width;    //expected width of label
    CGSize theStringSize = [self.text boundingRectWithSize:CGSizeMake(finalWidth, finalHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.font} context:nil].size;
    int newLinesToPad = (finalHeight  - theStringSize.height) / fontSize.height;
    for(int i=0; i<newLinesToPad; i++){
        self.text = [NSString stringWithFormat:@" \n%@",self.text];
    }
}
/**
 @method 获取指定宽度情况ixa，字符串value的高度
 @param value 待计算的字符串
 @param fontSize 字体的大小
 @param andWidth 限制字符串显示区域的宽度
 @result float 返回的高度
 */
+ (float) heightForString:(NSString *)str font:(UIFont *)font width:(float)width maxHeight:(float)maxHeight{
    CGSize contentTextSize = [str boundingRectWithSize:CGSizeMake(width, maxHeight) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return contentTextSize.height;
}

+ (float) widthForString:(NSString *)str font:(UIFont *)font height:(float)height maxWidth:(float)maxWidth{
    CGSize contentTextSize = [str boundingRectWithSize:CGSizeMake(maxWidth, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil].size;
    return contentTextSize.width;
}

@end
